﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using ZG_member.Model;
namespace ZG_member.BLL
{
	/// <summary>
	/// goods
	/// </summary>
	public partial class goods
	{
		private readonly ZG_member.DAL.goods dal=new ZG_member.DAL.goods();
		public goods()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}
        public string GetId(string name)
        {
            return dal.GetId(name);
        }
        /// <summary>
        /// 根据GoodsId获取实体对象
        /// </summary>
        /// <param name="goodsId"></param>
        /// <returns></returns>
        public ZG_member.Model.goods GetModelGoodsId(int goodsId)
        {
            return dal.GetModelgoodsId(goodsId);
        }
        public ZG_member.Model.goods getModelGoodsIdString(string goodsId)
        {
            return dal.GetModelgoodsIdString(goodsId);
        }
		/// <summary>
		/// 是否存在该记录
		/// </summary>
        public bool Exists(int id, string name)
        {
            return dal.Exists(id, name);
        }
        public bool Existsbyid(string id, string goodsname)
        {
            return dal.Existsbyid(id, goodsname);
        }
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(ZG_member.Model.goods model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ZG_member.Model.goods model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			return dal.Delete(id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			return dal.DeleteList(idlist );
		}
        public bool Deletewhere(string idlist)
        {
            return dal.Deletewhere(idlist);
        }
        public int GetbyId(string name)
        {
            return dal.GetbyId(name);

        }
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZG_member.Model.goods GetModel(int id)
		{
			
			return dal.GetModel(id);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public ZG_member.Model.goods GetModelByCache(int id)
		{
			
			string CacheKey = "goodsModel-" + id;
			object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(id);
					if (objModel != null)
					{
						int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
						Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (ZG_member.Model.goods)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
        /// <summary>
        /// 根据商品名称获得实体对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ZG_member.Model.goods GetModelName(string name)
        {
            return dal.GetModelName(name);
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<ZG_member.Model.goods> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<ZG_member.Model.goods> DataTableToList(DataTable dt)
		{
			List<ZG_member.Model.goods> modelList = new List<ZG_member.Model.goods>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				ZG_member.Model.goods model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new ZG_member.Model.goods();
					if(dt.Rows[n]["id"]!=null && dt.Rows[n]["id"].ToString()!="")
					{
						model.id=int.Parse(dt.Rows[n]["id"].ToString());
					}
					if(dt.Rows[n]["goodsId"]!=null && dt.Rows[n]["goodsId"].ToString()!="")
					{
					model.goodsId=dt.Rows[n]["goodsId"].ToString();
					}
					if(dt.Rows[n]["goodsName"]!=null && dt.Rows[n]["goodsName"].ToString()!="")
					{
					model.goodsName=dt.Rows[n]["goodsName"].ToString();
					}
					if(dt.Rows[n]["unit"]!=null && dt.Rows[n]["unit"].ToString()!="")
					{
					model.unit=dt.Rows[n]["unit"].ToString();
					}
					if(dt.Rows[n]["price"]!=null && dt.Rows[n]["price"].ToString()!="")
					{
						model.price=decimal.Parse(dt.Rows[n]["price"].ToString());
					}
                    //if (ds.Tables[0].Rows[0]["Promotionprice"] != null && ds.Tables[0].Rows[0]["Promotionprice"].ToString() != "")
                    //{
                    //    model.Promotionprice1 = ds.Tables[0].Rows[0]["Promotionprice"].ToString();
                    //}

                    //if (ds.Tables[0].Rows[0]["finalCost"] != null && ds.Tables[0].Rows[0]["finalCost"].ToString() != "")
                    //{
                    //    model.finalCost = decimal.Parse(ds.Tables[0].Rows[0]["finalCost"].ToString());
                    //}
                    //if (ds.Tables[0].Rows[0]["Promotion"] != null && ds.Tables[0].Rows[0]["Promotion"].ToString() != "")
                    //{
                    //    model.Promotion1 = ds.Tables[0].Rows[0]["Promotion"].ToString();
                    //}
                    if (dt.Rows[n]["Promotionprice"] != null && dt.Rows[n]["Promotionprice"].ToString() != "")
                    {
                        model.Promotionprice = dt.Rows[n]["Promotionprice"].ToString();
                    }
                    if (dt.Rows[n]["Promotion"] != null && dt.Rows[n]["Promotion"].ToString() != "")
					{
                        model.Promotion = Convert.ToBoolean( dt.Rows[n]["Promotion"]);
					}
                    if (dt.Rows[n]["goodsTypeId"] != null && dt.Rows[n]["goodsTypeId"].ToString() != "")
                    {
                        model.goodsTypeId = dt.Rows[n]["goodsTypeId"].ToString();
                    }

					if(dt.Rows[n]["amount"]!=null && dt.Rows[n]["amount"].ToString()!="")
					{
						model.amount=int.Parse(dt.Rows[n]["amount"].ToString());
					}

                    if (dt.Rows[0]["tichengType"] != null && dt.Rows[0]["tichengType"].ToString() != "")
                    {
                        model.tichengType = dt.Rows[0]["tichengType"].ToString();
                    }
                    if (dt.Rows[0]["finalCost"] != null && dt.Rows[0]["finalCost"].ToString() != "")
                    {
                        model.finalCost = Convert.ToDouble(dt.Rows[0]["finalCost"]);
                    }
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  Method
	}
}

